const app = getApp();
Page({
  data: {
    date: "请选择",
    sex: "男",
    sexIndex: '1',
    sexarray: ["男", "女"],
    username: '',
    addressInfo: '',
    areaInfo: '',
    perInfo: '',
    perExp: ''
  },
  onLoad: function (options) {
    var self = this;
    wx.getStorage({
      key: 'key',
      success: res => {
        self.data.key = res.data;
      }
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindSexChange: function (e) {
    let self = this;
    this.setData({
      sex: self.data.sexarray[e.detail.value]
    })
    this.data.sexIndex = e.detail.value * 1 + 1;
  },
  getName: function (e) {
    this.data.username = e.detail.value;
  },
  checkName: function (e) {
    let username = e.detail.value;
    if (username.length < 2) {
      wx.showToast({
        title: "请输入正确的姓名",
        icon: 'none'
      })
    }
  },
  getAddressinfo: function (e) {
    this.data.addressInfo = e.detail.value;
  },
  getArea: function (e) {
    this.data.areaInfo = e.detail.value;
  },
  getPerInfo: function (e) {
    this.data.perInfo = e.detail.value;
  },
  getPerExp: function (e) {
    this.data.perExp = e.detail.value;
  },
  apply: function () {
    let self = this;
    if (!this.data.username || !this.data.addressInfo || !this.data.areaInfo || !this.data.perInfo || !this.data.perExp || this.data.date == '请选择') {
      wx.showToast({
        title: '信息填写有误，请重新填写',
        icon: 'none'
      })
      return;
    }
    if (this.data.key) {
      wx.request({
        url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=commander_apply',
        data: {
          key: self.data.key,
          true_name: self.data.username,
          birthday: self.data.date,
          sex: self.data.sexIndex,
          address: self.data.addressInfo,
          target_area: self.data.areaInfo,
          personal_introduction: self.data.perInfo,
          promotion_experience: self.data.perExp,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '申请成功！',
              icon: 'success',
              duration: 1500
            })
            setTimeout(() => {
              wx.switchTab({
                url: '../member/member'
              })
            }, 1500);
          } else {
            wx.showToast({
              title: res.data.error,
              icon: 'none'
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请登录',
        icon: 'none'
      })
    }

  }
})